<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>第十节课--手绘板功能</title>
		<style type="text/css">
			*{
				padding: 0;
				margin: 0;
			}
			.container{
				width: 100vw;
				height: 100vh;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			canvas{
				width: 500px;
				height: 500px;
				border: 1px solid #333333;
			}
		</style>
	</head>
	<body>
		<div class="container">
			<canvas id="canvas" width="500" height="500"></canvas>
		</div>
	</body>
	<script>
		window.onload = function(){
			let yCanvas = document.getElementById('canvas');
			console.dir(yCanvas)
			let yPen = yCanvas.getContext("2d");
			console.dir(yPen)  
			
//			var e;
//			document.onmousedown = function(e) {
//				window.event;
//				yPen.moveTo(e.pageX - yCanvas.offsetLeft,e.pageY - yCanvas.offsetTop);
//			document.onmousemove = function(e) {
//				window.event;
//				yPen.lineTo(e.pageX - yCanvas.offsetLeft,e.pageY - yCanvas.offsetTop);
//	            yPen.stroke();
//				}
//			document.onmouseup = function(e) {
//				document.onmousemove = null;
//				document.onmouseup = null;
//				}
//			}
			
			let isMove = false,
				startX = 0,
				startY = 0;
				
			document.onmousemove = function(e) {
				let nowX = 0,
					nowY = 0;
					nowX = (e.pageX - yCanvas.offsetLeft);
					nowY= (e.pageY - yCanvas.offsetTop);
				if(isMove) {
					yPen.lineTo(nowX,nowY);
					yPen.stroke();
				}
			}
			
			document.onmousedown = function(e) {
				isMove = true;
				startX = (e.pageX - yCanvas.offsetLeft);
				startY = (e.pageY - yCanvas.offsetTop);
				yPen.moveTo(startX,startY);
				}
			
			document.onmouseup = function(e) {
				console.dir('x=' + (e.pageX - yCanvas.offsetLeft))
				console.dir('y=' + (e.pageY - yCanvas.offsetTop))
				isMove = false;
				}
		}
	</script>
</html>